package com.myqm.service.impl.weixin;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.myqm.dao.weixin.CarDao;
import com.myqm.dao.weixin.OrderDetailDao;
import com.myqm.dao.weixin.OriginalOrderDao;
import com.myqm.pojo.weixin.Car;
import com.myqm.pojo.weixin.OrderDetail;
import com.myqm.pojo.weixin.OriginalOrder;
import com.myqm.pojo.weixin.query.QueryOriginalOrder;
import com.myqm.service.weixin.OriginalOrderService;
import com.myqm.util.DateUtil;


@Service
public class OriginalOrderServiceImpl implements OriginalOrderService{

	@Autowired
	public OriginalOrderDao originalOrderDao;
	@Autowired
	public OrderDetailDao orderDetailDao;
	@Autowired
	public CarDao carDao;
	
    
	@Transactional
	@Override  
	public OriginalOrder add(OriginalOrder record, Integer customerId, Boolean delflag) {
		record.setCustomerId(customerId);
		record.setIsweb("2");
		String preFix="WX";
		preFix += DateUtil.getDays(); 
		int id = originalOrderDao.getMaxOrderNo(preFix+"____");
		id++;
		String no = String.format("%04d", id); 
	    record.setOrderNo(preFix + no);
		originalOrderDao.add(record);
		
		List<OrderDetail>details=record.getDetails();
		for(int i=0;i<details.size();i++) {
			details.get(i).setOrderId(record.getId());
			orderDetailDao.add(details.get(i));
		}
		if (delflag) {
			for(int i=0;i<details.size();i++) {	
				carDao.del(customerId, details.get(i).getInternaltradeId());
			}
		}
		return record;
	}

	@Override
	public OriginalOrder getById(Integer id) {
		OriginalOrder order =  originalOrderDao.getById(id);
		if(order !=null) {
			List<OrderDetail>details=orderDetailDao.listByOrderId(id);
			order.setDetails(details);
		}
		return order;		
	}


	
	@Override 
	public PageInfo<OriginalOrder> list(int pagenum, int pagesize, QueryOriginalOrder param) {
		PageHelper.startPage(pagenum, pagesize);
		List<OriginalOrder> datalist = originalOrderDao.list(param); 
		PageInfo<OriginalOrder> p = new PageInfo<OriginalOrder>(datalist);
		return p;
	}
	
	
	@Override 
	public PageInfo<OriginalOrder> getbyCustomerId(int pagenum, int pagesize,QueryOriginalOrder param) {
		PageHelper.startPage(pagenum, pagesize);
		List<OriginalOrder> datalist = originalOrderDao.getbyCustomerId(param); 
		PageInfo<OriginalOrder> p = new PageInfo<OriginalOrder>(datalist);
		return p;
	}
 

	@Override
	public int cancel(Integer id) {
		//已经审核的不能取消
		String isVerify=originalOrderDao.getById(id).getIsverify();
		if(isVerify=="1") 
			return -1; 
		return originalOrderDao.cancel(id); 
	}
	
	@Override 
	public int copyToCar(Integer id)
	{
		OriginalOrder order = originalOrderDao.getById(id);
		if (order == null) return -1;
		List<OrderDetail>details=orderDetailDao.listByOrderId(id);
		int customerId = order.getCustomerId();
		Car good= new Car();
		for(int i=0;i<details.size();i++)
		{
			good.setCustomerId(customerId);
			good.setInternalTradeId(details.get(i).getInternaltradeId());
			Double count = carDao.get(good.getCustomerId(), good.getInternalTradeId());
			if(count == null) {
				carDao.add(good);
			}else {
				count = details.get(i).getAmount() + good.getCount();
				good.setCount(count);
				carDao.editById(good);
			} 
		}
		return customerId;
	}

	
}

